import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker, message,Table,Button  } from 'antd';
import reqwest from 'reqwest';
var log, className = "dark";
var newCount = 1;
var CommonTree = React.createClass({
  getInitialState: function () {
		return {
			setting : {
			view: {
				addHoverDom: this.addHoverDom,
				removeHoverDom: this.removeHoverDom,
				selectedMulti: false
			},
			async: {
				enable: true,
				url:"http://192.168.2.9:8080/learn/s/resources/hResourceDir/list",
				autoParam:["id", "name=n", "level=lv"],
				otherParam:{"corpId":this.props.corpId,"resourceType":this.props.resourceType}
				//dataFilter: filter
			},
			callback: {
				onAsyncSuccess: this.onAsyncSuccess,
				onClick: this.zTreeOnClick,
				onRename: this.zTreeOnRename,
				beforeRemove: this.zTreeBeforeRemove,
				onRemove: this.zTreeOnRemove
			},
			edit: {
				enable: true,
				editNameSelectAll: true
				
			}
		}
		};
  },
  zTreeOnRemove:function (event, treeId, treeNode) {
	  reqwest({
				url:"http://192.168.2.9:8080/learn/s/resources/hResourceDir/remove?id="+treeNode.id, method: 'get',
				type: 'json',
				  success: function (resp) {
					  console.info(resp);
					  console.info(treeNode.tId + ", " + treeNode.name);
					},
					error: function (err) { 
						console.info(err);
					}
					
				});
		console.info(treeNode.tId + ", " + treeNode.name);
	},
  zTreeBeforeRemove:function (treeId, treeNode) {
	    if (confirm("您确认要删除 "+treeNode.name+" 目录和它相关的所有资源吗？"))
			return true;
		else
			return false;
	},
  zTreeOnRename:function (event, treeId, treeNode, isCancel) {
	  reqwest({
			url:"http://192.168.2.9:8080/learn/s/resources/hResourceDir/update?id="+treeNode.id+"&name="+encodeURI(encodeURI(treeNode.name)), method: 'get',
			type: 'json',
			  success: function (resp) {
				  console.info(resp);
				  console.info(treeNode.tId + ", " + treeNode.name);
				},
				error: function (err) { 
					console.info(err);
				}
				
			});
				
		
	},
  zTreeOnClick:function (event, treeId, treeNode) {
		console.info(treeNode.tId + ", " + treeNode.name);
	
	},
  onAsyncSuccess:function (event, treeId, treeNode, msg) {
			//showLog("[ "+getTime()+" onAsyncSuccess ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
			
		},
  addHoverDom:function (treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		var retThis=this;
		if (btn) btn.bind("click", function(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			
			reqwest({
				url:"http://192.168.2.9:8080/learn/s/resources/hResourceDir/add?id="+treeNode.id+"&name="+encodeURI(encodeURI("new node" + newCount))+"&corpId="+retThis.props.corpId+"&resourceType="+retThis.props.resourceType, method: 'get',
				type: 'json',
				  data: [{id: treeNode.id, name: encodeURI(encodeURI("new node" + newCount))}],
				  success: function (resp) {
					  console.info(resp);
					  zTree.addNodes(treeNode, {id:(resp.data), pId:treeNode.id, name:"new node" + (newCount++)});
					},
					error: function (err) { 
						console.info(err);
					}
					
				});
	
			return false;
		});
	},
	removeHoverDom:function (treeId, treeNode) {
			$("#addBtn_"+treeNode.tId).unbind().remove();
		},
	componentWillMount: function () {
	},
		
  componentDidMount: function () {
   $.fn.zTree.init($("#treeDemo"), this.state.setting);
  },
 


  render: function () {
    return (
     <div className="zTreeDemoBackground left">
		<ul id="treeDemo" className="ztree"></ul>
	</div>
    );
  }
});


export default CommonTree;